/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.tabel;

import java.util.List;
import org.opu.db_vdumper.ui.ViewMethod;
import org.opu.db_vdumper.beans.ColumnBean;
import org.opu.db_vdumper.ui.component.tabel.models.ArrowModel;
import org.opu.db_vdumper.ui.component.tabel.models.TableModel;
import org.opu.db_vdumper.ui.component.tabel.views.ArrowView;
import org.opu.db_vdumper.ui.component.tabel.views.TableView;

/**
 *
 * @author Potochnak Yana
 */
public class ArrowMethod extends ViewMethod {

    public ArrowView getArrowView(ArrowModel model, List<TableView> views){
        TableView from = null, to = null;

        for (TableView tv : views) {
        	TableModel m = tv.getViewModel();
			if (m == model.getFromModel() && m == model.getToModel()){
                from = tv;
                to = tv;
            } else if (m == model.getFromModel()){
                from = tv;
            } else if (m == model.getToModel()){
                to = tv;
            }

            if (from != null && to != null){
                break;
            }
        }

        if (from == null || to == null){
            throw new NullPointerException();
        }

        return getArrowView(model, from, to);
    }

    public ArrowView getArrowView(TableView from, TableView to, String name){
        ArrowModel model = getArrowModel(from.getViewModel().attribute(name),
                from.getViewModel(), to.getViewModel());

        if(model == null){
            return null;
        }

        return (ArrowView) initView(new ArrowView(model, from, to));
    }

    public ArrowView getArrowView(ArrowModel model, TableView from, TableView to){
        return (ArrowView) initView(new ArrowView(model, from, to));
    }

    public ArrowModel getArrowModel(ColumnBean bean, TableModel from, TableModel to){
        return new ArrowModel(from, to, bean, bean.getReferences());
    }

}
